//提示信息对象
let msg;
//列对象
let cols;
let URL = 'CloudFile/all?deId=' + $("#deid").val();

layui.config({
    base: '../lay/dist/'
});
layui.use(['form', 'table', 'laydate', 'notice'], function () {
    var $ = layui.jquery,
        form = layui.form,
        table = layui.table,
        laydate = layui.laydate,
        notice = layui.notice;
    msg = layui.notice;
    /**
     * 表格主题渲染
     */
    table.render({
        elem: '#CloudFile',
        url: URL,
        parseData: function (res) {
            return {
                "code": res.success == true ? 0 : 1,
                "count": res.total,
                "data": res.data

            }
        },
        toolbar: '#toolbarDemo',
        defaultToolbar: ['filter', 'exports', 'print', {
            title: '提示',
            layEvent: 'LAYTABLE_TIPS',
            icon: 'layui-icon-tips'
        }],
        cols: [
            [
                {
                    field: 'xid',  title: '序号', sort: true, templet: function (d) {
                        return d.LAY_INDEX;
                    }
                },
                {field: 'fileId',  title: 'ID', sort: true, hide: true},
                {
                    field: 'upTime', title: '上传日期', sort:true, templet: function (d) {
                        return layui.laytpl.toDateString(d.date);
                    }
                },
                {field: 'fileName', title: '文件名称',templet:function (d) {
                        return '<a style="color: #0f9ae0;text-decoration: underline" href="'+d.filePath+'">'+ d.fileName+'</a>'

                    }},
                {field: 'fileSize', title: '文件大小',templet:function (d) {
                        return (d.fileSize) / 1024 + "kb";

                    }},
                {field: 'deName', title: '文件来源'},
                {field: 'fileRelativePath', sort: true,hide: true} ,

                {title: '操作', minWidth: 150, toolbar: '#currentTableBar', align: "center" , fixed: 'right'}

            ]
        ],
        limits: [10, 15, 20, 25, 50, 100],
        limit: 15,
        page: true,
        skin: 'line'
    });
    /**
     * 监听搜索操作
     */
    form.on('submit(data-search-btn)', function (data) {
        var result = data.field;

        //执行搜索重载
        table.reload('CloudFile', {
            url: 'CloudFile/all_byTime'
            , where: {
                Time: result.upTime,
                deId:1
            }
        }, 'data');
        notice.success("搜索执行成功！");

        return false;
    });
    /**
     * toolbar监听事件
     */
    table.on('toolbar(currentTableFilter)', function (obj) {
        if (obj.event === 'selectAll') { //
            table.reload('checkClass', {
                url: '/checkClass/all'
            })
            notice.success("查询成功！");
        } else if (obj.event === 'selectIsRed') {  //
            table.reload('checkClass', {
                url: 'checkClass/IsRed',
                where: {
                    work: 90
                }
            })
            notice.success("查询成功！");
        }
    });
    /**
     * 工具行监听(删除/编辑)
     * */
    table.on('tool(currentTableFilter)', function (obj) {
        var data = obj.data;
        cols = obj;
        if (obj.event === 'edit') {
            console.log(data.id);

            var index = layer.open({
                title: '编辑用户',
                type: 2,
                shade: 0.2,
                maxmin: true,
                shadeClose: true,
                area: ['100%', '100%'],
                content: '/CheckClassEdit?id=' + data.id,
            });
            $(window).on("resize", function () {
                layer.full(index);
            });
            return false;
        } else if (obj.event === 'delete') {
            console.log("删除")
            layer.confirm('是否要删除此条记录？', function (index) {
                $.post("/checkClass/delete", {id: obj.data.id}, function (result) {
                    // console.log(result)
                    var isOK = result.total;
                    if (isOK != 1) {
                        notice.warning("删除失败！");
                        // console.log(isOK);

                        layer.open({
                            title: '提示'
                            , content: '删除失败！'
                        });
                    } else {
                        notice.success("删除成功！");
                        obj.del()
                    }
                })
                layer.close(index);
            }, function (index) {
                layer.close(index);
            });

        }

    });
    /**
     * 时间戳的处理
     * @param d
     * @param format
     * @returns {string}
     */
    layui.laytpl.toDateString = function (d, format) {
        var date = new Date(d || new Date())
            , ymd = [
            this.digit(date.getFullYear(), 4)
            , this.digit(date.getMonth() + 1)
            , this.digit(date.getDate())
        ]
            , hms = [
            this.digit(date.getHours())
            , this.digit(date.getMinutes())
            , this.digit(date.getSeconds())
        ];

        format = format || 'yyyy-MM-dd ';

        return format.replace(/yyyy/g, ymd[0])
            .replace(/MM/g, ymd[1])
            .replace(/dd/g, ymd[2])
            .replace(/HH/g, hms[0])
            .replace(/mm/g, hms[1])
            .replace(/ss/g, hms[2]);
    };
    /**
     * 数字前置补零
     * @param num
     * @param length
     * @param end
     * @returns {*}
     */
    layui.laytpl.digit = function (num, length, end) {
        var str = '';
        num = String(num);
        length = length || 2;
        for (var i = num.length; i < length; i++) {
            str += '0';
        }
        return num < Math.pow(10, length) ? str + (num | 0) : num;
    };

    /**
     * 时间选择器自定义颜色
     */
    laydate.render({
        elem: '#upTime'
        , theme: '#393D49'
    });

    /**
     * 设置提示信息属性
     * @type {{hideEasing: string, positionClass: string, hideDuration: string, debug: boolean, closeButton: boolean, extendedTimeOut: string, showEasing: string, onclick: null, showDuration: string, iconClass: string, timeOut: string}}
     */
    notice.options = {
        closeButton: true,//显示关闭按钮
        debug: false,//启用debug
        positionClass: "toast-bottom-right",//弹出的位置,
        showDuration: "300",//显示的时间
        hideDuration: "1000",//消失的时间
        timeOut: "2000",//停留的时间
        extendedTimeOut: "1000",//控制时间
        showEasing: "swing",//显示时的动画缓冲方式
        hideEasing: "linear",//消失时的动画缓冲方式
        iconClass: 'toast-info', // 自定义图标，有内置，如不需要则传空 支持layui内置图标/自定义iconfont类名
        onclick: null, // 点击关闭回调
    };

    // notice.warning("");信息状态
    // notice.info("");
    // notice.error("");
    // notice.success("");
});
/**
 * 子窗体编辑后父窗体动态改变
 * @param result
 * @param childData
 */
function parentPage(result, childData) {

    if (result.success == true) {
        cols.update({
            date: childData.beginTime,
            istop: childData.isTop,
            classRoomName: childData.classRoomName,
            className: childData.className,
            factPersons: childData.factPersons,
            work: childData.factPersonsWork,
            formalsuit: childData.formalsuit,
            formalwork: childData.formalwork,
            studentName: childData.studentName
        })

        msg.info("修改成功");
        return;
    }
    msg.warning("修改失败")

}
